package com.warm.pump.module.monitor.service.impl;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.gen.util.TypeCast;
import com.warm.pump.frame.util.StringUtils;
import com.warm.pump.module.monitor.bean.po.gen.MonitorCache;
import com.warm.pump.module.monitor.dao.gen.MonitorCacheDao;
import com.warm.pump.module.monitor.service.IMonitorCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("MonitorCacheService")
public class MonitorCacheServiceImpl implements IMonitorCacheService {
    @Autowired
    private MonitorCacheDao monitorCacheDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public MonitorCache queryObject(Long id) {
        return monitorCacheDao.selectByPrimaryKey(id);
    }

    public MonitorCache queryObject(Map<String, Object> map) {
        return monitorCacheDao.selectByMap(map);
    }

    @Override
    public List<MonitorCache> queryList(Map<String, Object> map) {
        return monitorCacheDao.getListByMap(map);
    }

    @Override
    public PageBean getPageBeanByMap(Map map, int pageNo, int pageSize) {
        return monitorCacheDao.getPageBeanByMap(map, pageNo, pageSize);
    }

    @Override
    public List<Map<String, Object>> getCacheKeys(String cacheName, String keyName) {
        List<Map<String, Object>> keyList = new ArrayList<>();

        if (StringUtils.isEmpty(cacheName)) {
            return keyList;
        }

        if (StringUtils.isNotEmpty(keyName)) {
            Object cacheValue = redisTemplate.opsForHash().get(cacheName, keyName);
            if (null != cacheValue) {
                Map<String, Object> keyMap = new HashMap<>();
                keyMap.put("keyName", keyName);
                keyList.add(keyMap);
            }
            return keyList;
        }

        Set<Object> keys = redisTemplate.opsForHash().keys(cacheName);
        if (null != keys && keys.size() > 0) {
            int i = 0;
            for (Object key : keys) {
                if (i >= 100) {
                    break;
                }
                Map<String, Object> keyMap = new HashMap<>();
                keyMap.put("keyName", key);
                keyList.add(keyMap);
                i++;
            }
        }
        keys.clear();
        return keyList;
    }

    @Override
    public void deleteCacheInfo(String cacheName, String keyName) {
        if (StringUtils.isEmpty(cacheName)) {
            return;
        }

        if (StringUtils.isNotEmpty(keyName)) {
            Object cacheValue = redisTemplate.opsForHash().get(cacheName, keyName);
            if (null != cacheValue) {
                redisTemplate.opsForHash().delete(cacheName, keyName);
                return;
            }
        }
        redisTemplate.delete(cacheName);

        Map<String, Object> condMap = new HashMap<>();
        condMap.put("startDate_lessEqual", new Date());
        condMap.put("expireDate_greaterEqual", new Date());
        condMap.put("cacheName", cacheName);
        List<MonitorCache> oldCacheList = monitorCacheDao.getListByMap(condMap);
        if (null != oldCacheList && oldCacheList.size() > 0) {
            for (MonitorCache oldCache : oldCacheList) {
                oldCache.setExpireDate(new Date());
                monitorCacheDao.updateByPrimaryKeySelective(oldCache);
            }
        }
    }

    @Override
    public Map<String, Object> getCacheInfo(String keyName, String cacheName) {
        String cacheValue = TypeCast.getString(redisTemplate.opsForHash().get(cacheName, keyName));
        Map<String, Object> cacheInfo = new HashMap<>();
        cacheInfo.put("cacheName", cacheName);
        cacheInfo.put("cacheKey", keyName);
        cacheInfo.put("cacheValue", cacheValue);
        return cacheInfo;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return monitorCacheDao.countTotalByMap(map);
    }

    @Override
    public void save(MonitorCache monitorCache) {
        monitorCacheDao.insertSelective(monitorCache);
    }

    @Override
    public void update(MonitorCache monitorCache) {
        monitorCacheDao.updateByPrimaryKeySelective(monitorCache);
    }

    @Override
    public void delete(Long id) {
        monitorCacheDao.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(Map<String, Object> map) {
        monitorCacheDao.deleteByMap(map);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("id_in", ids);
        monitorCacheDao.deleteByMap(map);
    }

}
